﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KBManagement.DAL;
using KBManagement.Library;
namespace KBManagement.BIZ
{
    class CustomerFacade
    {
        public bool AddNewCustomerType(Customer_Type customerType)
        {
            try
            {
                var context = Utils.CreateInstance().GetDBContext();
                context.Customer_Types.InsertOnSubmit(customerType);
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public Customer Find(object ID) {
            var context = Utils.CreateInstance().GetDBContext();
            try
            {
                return context.Customers.Single(x => x.ID.Equals(ID));
            }
            catch (Exception)
            {
                return null;
            }
        }
        public Customer FindByPhone(string phone)
        {
            var context = Utils.CreateInstance().GetDBContext();
            try
            {
                return context.Customers.Single(x => x.Phone == phone);
            }
            catch (Exception)
            {
                return null;
            }
            
        }
        public Customer[] FindByName(string name)
        {
            var context = Utils.CreateInstance().GetDBContext();
            var customers = from c in context.Customers where c.FullName.Contains(name) select c;
            return customers.ToArray();
        }
        public IQueryable FindMany(string customertypeID)
        {
            var context = Utils.CreateInstance().GetDBContext();
            var customers = from c in context.Customers where c.CustomeTypeID == customertypeID select c;
            return customers;
        }
    }
}
